home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6013 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  58 lines

  1. Path: alfa.ist.utl.pt!l40128
  2. From: l40128@alfa.ist.utl.pt (delphis)
  3. Newsgroups: comp.lang.c
  4. Subject: Binary files ?
  5. Date: 22 Feb 1996 09:53:18 GMT
  6. Organization: Instituto Superior Tecnico 
  7. Message-ID: <4gheee$bvr@ci.ist.utl.pt>
  8. NNTP-Posting-Host: alfa.ist.utl.pt
  9. X-Newsreader: TIN [version 1.2 PL2]
  10.  
  11.  
  12.  
  13.  
  14.  
  15.       Greetings from a frustrated c programmer!
  16.       I program in c a couple of years but i never figure out this basic 
  17. question : how to work with binary files ? basically, what i want to do 
  18. is writing a matrix to a file and read it back but when i try the 
  19. following code in dos the matrix comes all gobled, but works fine in unix!
  20.       Can anyone tell me whats wrong with this ? I have been using text files
  21. and they dont give any troble at all!
  22.  
  23. Obrigadissimo!
  24.  
  25.  
  26.  
  27.  
  28. # include <stdio.h>
  29.  
  30.  
  31. void main()
  32. {
  33.  
  34.  /*write file*/
  35.  int n;
  36.  unsigned char mat[200]; 
  37.  FILE *f=fopen("teste","w"); /*do i have to use the CREATE before this ?*/
  38.  for (n=0;n<200;n++) mat[n]=n; /*init matrix*/
  39.  fwrite(mat,200,1,f);
  40.  fclose(f);
  41.  
  42.  /*reset matrix*/
  43.  for (n=0;n<200;n++) mat[n]=0;
  44.  
  45.  /*read file*/
  46.  f=fopen("teste","r");
  47.  fread(mat,200,1,f);
  48.  fclose(f);
  49.  for (n=0;n<200;n++) printf("%d ",mat[n]);/*print it*/
  50. }
  51.  
  52.  
  53.  
  54. // The main difference between man and animal is that man possesses the \\
  55. //hability to think he is rational...                                   \\
  56. E-mail : l40128@alfa.ist.utl.pt
  57.  
  58.